package com.xbn.netty.nio;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author XBN
 * @date 2020/10/13 17:44
 * @email 78742541@qq.com
 */
public class FileChannelCopy {
    public static void main(String[] args) throws IOException {
        FileInputStream inputStream = new FileInputStream("1.txt");
        FileChannel inputStreamChannel = inputStream.getChannel();
        FileOutputStream fileOutputStream = new FileOutputStream("2.txt");
        FileChannel outputStreamChannel = fileOutputStream.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(512);
        while (true)
        {
            //clear方法一定要调用
            byteBuffer.clear();
            int read = inputStreamChannel.read(byteBuffer);
            if(read==-1) break;
            byteBuffer.flip();
            outputStreamChannel.write(byteBuffer);
        }
        inputStream.close();
        fileOutputStream.close();
    }
}
